/*
 * Copyright (C), 2002-2017, 上海亦寻信息技术有限公司
 * FileName: LevelStaticSalesDataServiceImpl.java
 * Author:   linyao
 * Date:     2017-3-8 下午2:43:14
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.emm.yixun.website.service.pmo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.emm.yixun.common.enums.ResponseEnums;
import com.emm.yixun.common.model.LevelStaticSalesData;
import com.emm.yixun.common.model.dto.PmoLogDto;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.utils.MD5;
import com.emm.yixun.common.utils.RequestUtil;
import com.emm.yixun.common.utils.ResponseBody;
import com.emm.yixun.system.service.pmo.LevelStaticSalesDataServiceFacade;
import com.emm.yixun.website.constant.PmoConfig;

/**
 * 〈一句话功能简述〉 〈功能详细描述〉
 * 
 * @author linyao
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service
public class LevelStaticSalesDataServiceImpl implements LevelStaticSalesDataService {
	private final Logger logger = LoggerFactory.getLogger(LevelStaticSalesDataServiceImpl.class); 
	
	@Autowired
	private LevelStaticSalesDataServiceFacade levelStaticSalesDataServiceFacade;

	@Autowired
	private PmoConfig pmoConfig;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.emm.yixun.website.service.pmo.LevelStaticSalesDataService#updateProjectInfo(com.emm.yixun.common.model.
	 * LevelProjectData)
	 */
	@Override
	public Response<?> updateProjectInfo(Long merchantId, Long projectId) {
		Response<?> res = new Response<Object>();
		try {
			Response<LevelStaticSalesData> response = levelStaticSalesDataServiceFacade.updateProjectInfo(merchantId,
					projectId,true);
			String pomLogId = response.getResultMessage();
			if (response.isSuccess()) {
				LevelStaticSalesData levelStaticSalesData = response.getEntity();
				String clientKey = pmoConfig.getClientKey();
				String seq = pmoConfig.getSeq();
				String digest = MD5.md5(clientKey + seq + pmoConfig.getClientPwd()).toUpperCase();

				String json = JSON.toJSONString(levelStaticSalesData);
				ResponseBody responseBody = RequestUtil.buildRequest(
						json,
						pmoConfig.getRequest_url() + pmoConfig.getStatic_sale_request()
								+ levelStaticSalesData.getProjectCode(),clientKey,seq,digest);
				PmoLogDto pmoLogDto = new PmoLogDto();
				pmoLogDto.setId(Long.valueOf(pomLogId));
				if (responseBody.getCode().equals(ResponseEnums.RESPONSEENUMS_000000.getCode())) {// 成功
					pmoLogDto.setStatus("1");
					res.setErrorCode("0000");
					res.setSuccess(true);
				} else {
					pmoLogDto.setStatus("2");
					res.setErrorCode("9999");
					res.setSuccess(false);
				}
				pmoLogDto.setReturnResult(JSON.toJSONString(responseBody));
				levelStaticSalesDataServiceFacade.updatePmoLog(pmoLogDto);
				
				//返回信息
				res.setResultMessage(responseBody.getDesc());
				return res;
			}else{
				PmoLogDto pmoLogDto = new PmoLogDto();
				pmoLogDto.setId(Long.valueOf(pomLogId));
				pmoLogDto.setStatus("2");
				pmoLogDto.setReturnResult("销售主数据接口组装参数错误");
				levelStaticSalesDataServiceFacade.updatePmoLog(pmoLogDto);
				
				res.setSuccess(false);
				res.setErrorCode("9999");
				res.setResultMessage("静态销售主数据接口组装参数错误");
				return res;
			}
		} catch (Exception e) {
			logger.error("SendStaticSalePmo error===", e);
			
			res.setSuccess(false);
			res.setErrorCode("9999");
			res.setResultMessage("静态销售主数据接口组装参数错误");
			return res;
		}
	}

}
